package v1

import (
	"bc/dao"
	"bc/service"
	"bc/util"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type menu struct {

}
func (t *menu)Create(c *gin.Context){

	d := map[string]interface{}{}
	if v,ok := c.GetPostForm("f_id");ok{
		f_id,err := strconv.Atoi(v)
		if err != nil{
			c.JSON(200,gin.H{
				"code":500,
				"msg":"error",
				"data":"数据有误，请重试",
			})
			return
		}
		d["f_id"] = f_id
	}
	if v,ok := c.GetPostForm("name");ok{
		d["name"] = v
	}
	if v,ok := c.GetPostForm("icon");ok{
		d["icon"] = v
	}
	if v,ok := c.GetPostForm("desc");ok{
		d["desc"] = v
	}
	if v,ok := c.GetPostForm("path");ok{
		d["path"] = v
	}

	w := map[string]interface{}{
		"name" : d["name"],
	}

	b,_ := service.Menu.First(w)
	code := 200
	msg := "操作成功"
	data := "成功"

	if b == true {
		code = 500
		msg = "error"
		data = "该菜单名称已存在"
	}else {
		bool,id := service.Menu.Create(d)

		if !bool {
			code = 500
			msg = "error"
			data = "失败"
		}
		LogAdd(c,"添加菜单" + strconv.Itoa(id),"菜单管理")
	}


	c.JSON(200,gin.H{
		"code":code,
		"msg":msg,
		"data":data,
	})
}
func (t *menu)Delete(c *gin.Context){

	w := map[string]interface{}{}
	m_id := c.Param("m_id")

	if m_id == ""{
		c.JSON(http.StatusOK,gin.H{
			"code":500,
			"msg":"error",
			"data":"参数有误，请重试",
		})
		return
	}
	w["m_id"] = m_id
	b := service.Menu.Delete(w)
	if b {
		LogAdd(c,"删除菜单" + m_id,"菜单管理")
		c.JSON(200,gin.H{
			"code":200,
			"msg":"操作成功",
			"data":"成功",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
		"data":"失败",
	})

}
func (t *menu)Update(c *gin.Context){

	w := make(map[string]interface{})
	f := make(map[string]interface{})

	m_id := c.Param("m_id")
	if m_id == ""{
		c.JSON(http.StatusOK,gin.H{
			"code":500,
			"msg":"error",
			"data":"参数有误，请重试",
		})
		return
	}
	w["m_id"] = m_id

	if v,ok := c.GetPostForm("f_id");ok{
		f["f_id"] = v
	}
	if v,ok := c.GetPostForm("name");ok{
		f["name"] = v
	}
	if v,ok := c.GetPostForm("icon");ok{
		f["icon"] = v
	}
	if v,ok := c.GetPostForm("desc");ok{
		f["desc"] = v
	}
	if v,ok := c.GetPostForm("path");ok{
		f["path"] = v
	}

	b := service.Menu.Update(w,f)
	if b {
		LogAdd(c,"更新菜单" + m_id,"菜单管理")
		c.JSON(200,gin.H{
			"code":200,
			"msg":"操作成功",
			"data":"成功",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
		"data":"失败",
	})

}
func (t *menu)Find(c *gin.Context){

	w := make(map[string]interface{})
	page := int64(1)
	if v,ok := c.GetPostForm("page");ok{
		p,_ := strconv.ParseInt(v,10,64)
		page = p
	}

	pageSize := util.Config.App.PageSize
	result,total,err := service.Menu.Find(w,page,pageSize)

	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"数据有误，请重试",
		})
		return
	}

	data,_ := json.Marshal(result)

	//fmt.Println(string(data))
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":string(data),
		"total":total,
	})

}
func (t *menu)First(c *gin.Context){

	w := make(map[string]interface{})
	m_id := c.Param("m_id")
	if m_id == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"参数错误",
		})
		return
	}
	w["m_id"] = m_id
	b,data := service.Menu.First(w)
	if !b {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"错误，请重试",
		})
		return
	}

	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":data,
	})

}
func (t *menu)Nodes(c *gin.Context){

	source := service.Menu.Nodes()
	d := dao.MenuDao.Trees(0,source)
	byte,err  := json.Marshal(d)
	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"数据错误，请重试",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":string(byte),
	})

}
var Menu = &menu{}
